package com.ysc.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysc.task.constant.YSConstant;
import com.ysc.task.entity.Task;
import com.ysc.task.entity.dto.TaskAddDTO;
import com.ysc.task.exception.YscException;
import com.ysc.task.mapper.TaskMapper;
import com.ysc.task.service.ITaskService;
import com.ysc.task.task.DynamicTaskManager;
import com.ysc.task.util.CronUtils;
import com.ysc.task.util.EmailTool;
import com.ysc.task.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Version: 1.0
 * Class Name: TaskServiceImpl
 * Description: TODO(一句话描述功能)
 * Author: ysc
 * Date: 2024/3/31 13:20
 */
@Service
@RequiredArgsConstructor
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    private final TaskMapper taskMapper;
    private final DynamicTaskManager dynamicTaskManager;
    private final EmailTool emailTool;

    @Override
    public R add(TaskAddDTO taskAddDTO) {
        Task task = new Task();
        BeanUtils.copyProperties(taskAddDTO, task, Task.class);

        if ( !CronUtils.checkCronValid(taskAddDTO.getTaskCron()) ) {
            throw new YscException(YSConstant.CRON_ERROR);
        }

        int insert = taskMapper.insert(task);
        if ( insert < 1 ) {
            throw new YscException(YSConstant.NETWORK_ERROR);
        }
        String id = task.getTaskId().toString();

        if (taskAddDTO.getTaskType() == 0) {
            dynamicTaskManager.addTask(id, () -> {
                remove(task.getTaskId());
                emailTool.sendEmail(taskAddDTO.getAcceptEmail(),
                        taskAddDTO.getTaskDescription(),
                        taskAddDTO.getSendContent());
            }, taskAddDTO.getTaskCron());
        } else {
            dynamicTaskManager.addTask(id, () -> emailTool.sendEmail(taskAddDTO.getAcceptEmail(),
                    taskAddDTO.getTaskDescription(),
                    taskAddDTO.getSendContent()), taskAddDTO.getTaskCron());
        }
        return R.ok();
    }

    @Override
    public R delete(Long id) {
        remove(id);
        return R.ok();
    }

    @Override
    public R enable(Long id) {
        update(id, 1);
        return R.ok();
    }

    @Override
    public R unEnable(Long id) {
        update(id, 0);
        return R.ok();
    }

    @Override
    public R getList(Long id) {
        List<Task> taskList = taskMapper.selectList(new LambdaQueryWrapper<Task>().eq(Task::getUserId, id));
        return R.ok().data("taskList", taskList);
    }

    public void update(Long taskId, Integer status) {
        String id = taskId.toString();
        Task task = new Task();
        task.setTaskId(taskId);
        task.setIsEnable(status);
        taskMapper.update(task, new LambdaQueryWrapper<Task>().eq(Task::getTaskId, taskId));
        if ( status == 1 ) {
            Task t = taskMapper.selectById(taskId);
            dynamicTaskManager.addTask(id, () -> {
                emailTool.sendEmail(t.getAcceptEmail(), t.getTaskDescription(), t.getSendContent());
                if ( t.getTaskType() == 0 ) {
                    remove(t.getTaskId());
                }
            }, t.getTaskCron());
        } else {
            dynamicTaskManager.removeTask(taskId.toString());
        }
    }

    public void remove(Long id) {
        dynamicTaskManager.removeTask(id.toString());
        taskMapper.deleteById(id);
    }
}
